package com.cwj.we.module.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.cwj.we.R;
import com.cwj.we.base.BaseActivity;
import com.cwj.we.base.BasePresenter;
import com.cwj.we.utils.TimeUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by cwj on 2024-11-30
 * Description: 孕产期
 */
public class YuChanQiActivity extends BaseActivity {

    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.llHead)
    LinearLayout llHead;
    @BindView(R.id.tvJieGuo)
    TextView tvJieGuo;
    @BindView(R.id.etBPD)
    EditText etBPD;
    @BindView(R.id.etAC)
    EditText etAC;
    @BindView(R.id.etFL)
    EditText etFL;
    @BindView(R.id.btnTiZhong)
    Button btnTiZhong;
    @BindView(R.id.tvMoCi)
    TextView tvMoCi;
    @BindView(R.id.etZhouQi)
    EditText etZhouQi;
    @BindView(R.id.btnYuChanQi)
    Button btnYuChanQi;

    private TimePickerView pvTime;
    private String moCiTime;
    private Calendar selectedDate;
    private SimpleDateFormat chineseDateFormat;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_yu_chan_qi;
    }

    @Override
    public void initData() {
        long nowTime = TimeUtils.getTimeStame();
        moCiTime = TimeUtils.dateToString(nowTime, "yyyy-MM-dd");
        tvMoCi.setText(moCiTime);

        selectedDate = Calendar.getInstance();//系统当前时间
    }

    @Override
    public void initView() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true)  //状态栏字体是深色，不写默认为亮色
                .init();
        llHead.setBackgroundColor(getResources().getColor(R.color.white));
        tvTitle.setText("预产期");
    }

    @SuppressLint("SetTextI18n")
    @OnClick({R.id.ll_back, R.id.btnTiZhong, R.id.btnYuChanQi, R.id.tvMoCi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.btnTiZhong:
                //估算胎宝宝体重
                if (TextUtils.isEmpty(etBPD.getText().toString().trim())) {
                    Toast.makeText(this, "请输入双顶径", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(etAC.getText().toString().trim())) {
                    Toast.makeText(this, "请输入腹围", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(etFL.getText().toString().trim())) {
                    Toast.makeText(this, "请输入股骨长", Toast.LENGTH_SHORT).show();
                } else {
                    tvJieGuo.setVisibility(View.VISIBLE);
                    double bpd = Double.parseDouble(etBPD.getText().toString().trim());
                    double ac = Double.parseDouble(etAC.getText().toString().trim());
                    double fl = Double.parseDouble(etFL.getText().toString().trim());
                    double tz = (1.07 * (bpd / 10) * (bpd / 10) * (bpd / 10) + 0.3 * (ac / 10) * (ac / 10) * (fl / 10)) * 2 / 1000;
                    DecimalFormat decimalFormat = new DecimalFormat("#.##");
                    String formattedNumber = decimalFormat.format(tz);
                    tvJieGuo.setText("胎宝宝体重大约重：" + formattedNumber + "斤");
                }
                break;
            case R.id.tvMoCi:
                //末次月经时间选择
                Calendar startDate = Calendar.getInstance();
                //正确设置方式 原因：注意事项有说明
                startDate.set(1900, 0, 1);
                //时间选择器
                pvTime = new TimePickerBuilder(this, (date, v) -> {
                    moCiTime = getTime2(date);
                    Log.i("aaa", "onViewClicked: "+moCiTime);
                    tvMoCi.setText(moCiTime);
                })
                        .setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                        .setRangDate(startDate, selectedDate)//起始终止年月日设定
                        .setDividerColor(Color.RED)//当前选中日期线条颜色
                        .setSubmitColor(Color.RED)//确定按钮文字颜色
                        .setCancelColor(Color.RED)//取消按钮文字颜色
                        .build();
                Calendar settTTime = Calendar.getInstance();
                chineseDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    settTTime.setTime(chineseDateFormat.parse(moCiTime));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                pvTime.setDate(TextUtils.isEmpty(moCiTime) ? selectedDate : settTTime);// 如果不设置的话，默认是系统时间*/
                pvTime.show();
                break;
            case R.id.btnYuChanQi:
                //估算预产期
                if (TextUtils.isEmpty(tvMoCi.getText().toString().trim())) {
                    Toast.makeText(this, "请选择末次月经日期", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(etZhouQi.getText().toString().trim())) {
                    Toast.makeText(this, "请输入月经周期", Toast.LENGTH_SHORT).show();
                } else {
                    tvJieGuo.setVisibility(View.VISIBLE);
                    int y = Integer.parseInt(tvMoCi.getText().toString().trim().substring(0, 4));
                    int m = Integer.parseInt(tvMoCi.getText().toString().trim().substring(5, 7));
                    int d = Integer.parseInt(tvMoCi.getText().toString().trim().substring(8, 10));
                    int yJieGuo;
                    int mJieGuo;
                    if (m > 3) {
                        yJieGuo = y + 1;
                        mJieGuo = m - 3;
                    } else {
                        yJieGuo = y;
                        mJieGuo = m + 9;
                    }
                    Solar ymd = Solar.fromYmd(yJieGuo, mJieGuo, d);
                    Lunar lunar = Lunar.fromDate(new Date());
                    Solar now = Solar.fromYmd(lunar.getYear(), lunar.getMonth() + 1, lunar.getDay());
                    Solar moCi = Solar.fromYmd(y, m, d);
                    if (Integer.parseInt(etZhouQi.getText().toString().trim()) <= 26) {
                        if ((now.subtract(moCi) - 2) % 7 <= 0) {
                            tvJieGuo.setText("预产期为：" + ymd.next(5)
                                    + "\n" + "胎儿周数为：" + (now.subtract(moCi) - 2) / 7 + "周");
                        } else {
                            tvJieGuo.setText("预产期为：" + ymd.next(5)
                                    + "\n" + "胎儿周数为：" + (now.subtract(moCi) - 2) / 7 + "周"
                                    + (now.subtract(moCi) - 2) % 7 + "天");
                        }
                    } else if (Integer.parseInt(etZhouQi.getText().toString().trim()) >= 35) {
                        if ((now.subtract(moCi) + 7) % 7 == 0) {
                            tvJieGuo.setText("预产期为：" + ymd.next(14)
                                    + "\n" + "胎儿周数为：" + (now.subtract(moCi) + 7) / 7 + "周");
                        } else {
                            tvJieGuo.setText("预产期为：" + ymd.next(14)
                                    + "\n" + "胎儿周数为：" + (now.subtract(moCi) + 7) / 7 + "周"
                                    + (now.subtract(moCi) + 7) % 7 + "天");
                        }
                    } else {
                        if (now.subtract(moCi) % 7 == 0) {
                            tvJieGuo.setText("预产期为：" + ymd.next(7)
                                    + "\n" + "胎儿周数为：" + now.subtract(moCi) / 7 + "周");
                        } else {
                            tvJieGuo.setText("预产期为：" + ymd.next(7)
                                    + "\n" + "胎儿周数为：" + now.subtract(moCi) / 7 + "周"
                                    + now.subtract(moCi) % 7 + "天");
                        }
                    }
                }
                break;
        }
    }

    private String getTime2(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}
